package org.esria.samples.dashboard.managers
{
	import flash.utils.Dictionary;
	
	import org.flexlib.mdi.containers.MDICanvas;
	
	import mx.collections.ArrayCollection;
	import mx.containers.Canvas;
	import mx.containers.ViewStack;
	import mx.controls.Alert;
	import mx.core.Container;
	import mx.events.FlexEvent;
	import mx.events.IndexChangedEvent;
	import mx.events.ItemClickEvent;
	
	import org.esria.samples.dashboard.events.LayoutChangeEvent;
	import org.esria.samples.dashboard.view.Pod;
	import org.esria.samples.dashboard.view.PodContentBase;
	

	public class PodContainerManager
	{
	/* 	public function PodContainerManager()
		{
		} */

		// Array of PodLayoutManagers
		public var podLayoutManagers:Array=new Array();

		// Stores the xml data keyed off of a PodLayoutManager.
		private var podDataDictionary:Dictionary=new Dictionary();

		// Stores PodLayoutManagers keyed off of a Pod.
		// Used for podLayoutManager calls after pods have been created for the first time.
		// Also, used for look-ups when saving pod content ViewStack changes.
		private var podHash:Object=new Object();


		//unused variables
		private var podLen:int=0;
		private var unsavedPodCount:Number=0;

		public function PodContainerManager(viewStack: ViewStack, mypods:ArrayCollection)
		{
			if (mypods != null && mypods.length > 0)
			{
				var containerWindowManagerHash:Object=new Object();
				var canvas:MDICanvas=new MDICanvas();
	
				// PodLayoutManager handles resize and should prevent the need for
				// scroll bars so turn them off so they aren't visible during resizes.
				//canvas.horizontalScrollPolicy="off";
			//	canvas.verticalScrollPolicy="off";
			//	canvas.label="Tab1";
				canvas.percentWidth=100;
				canvas.percentHeight=100;
				viewStack.addChild(canvas);
	
				// Create a manager for each view.
				var manager:PodLayoutManager=new PodLayoutManager(canvas);
				manager.container=canvas;

				manager.addEventListener(LayoutChangeEvent.UPDATE, StateManager.setPodLayout);
				podDataDictionary[manager]="";
				podLayoutManagers.push(manager);
				var index:Number=StateManager.getViewIndex();
				onItemClickTabBar(new ItemClickEvent(ItemClickEvent.ITEM_CLICK, false, false, null, index),viewStack, mypods);
			}
		}


		private function onItemClickTabBar(e:ItemClickEvent, viewStack: ViewStack, mypods:ArrayCollection):void
		{
			var index:Number=e.index;
			StateManager.setViewIndex(index); // Save the view index.
			viewStack.selectedIndex=index;

			// If data exists then add the pods. After the pods have been added the data is cleared.
			var podLayoutManager:PodLayoutManager=podLayoutManagers[index];
			if (podDataDictionary[podLayoutManager] != null)
			{
				var pod:PodVo;
				for (var i:int = 0 ; i < mypods.length ; i++)
				{
					pod = mypods[i] as PodVo;
					addPods(podLayoutManagers[index], pod.pod, pod.id, pod.title);
				}
			}
		}


		// Adds the pods to a view.
		public function addPods(manager:PodLayoutManager, podContent:Container, podId:String, podTitle:String):void
		{
			if (podContent != null)
			{
				var viewId:String="";
				var podId:String=podId;

				var pod:Pod=new Pod();
				pod.id=podId;
				pod.title=podTitle;
				pod.addElement(podContent);

				var index:Number;
				if (StateManager.isPodMinimized(viewId, podId))
				{
					index=StateManager.getMinimizedPodIndex(viewId, podId);
					manager.addMinimizedItemAt(pod, index);
				}
				else
				{
					index=StateManager.getPodIndex(viewId, podId);

					// If the index hasn't been saved move the pod to the last position.
					if (index == -1)
					{
						index=podLen + unsavedPodCount;
						unsavedPodCount+=1;
					}

					manager.addItemAt(pod, index, StateManager.isPodMaximized(viewId, podId));
				}

				pod.addEventListener(IndexChangedEvent.CHANGE, onChangePodView);

				podHash[pod]=manager;
			}

			// Delete the saved data.
			delete podDataDictionary[manager];

			// Listen for the last pod to complete so the layout from the ContainerWindowManager is done correctly. 
			pod.addEventListener(FlexEvent.UPDATE_COMPLETE, onCreationCompletePod);
		}




		// Pod has been created so update the respective PodLayoutManager.
		private function onCreationCompletePod(e:FlexEvent):void
		{
			try
			{
				e.currentTarget.removeEventListener(FlexEvent.UPDATE_COMPLETE, onCreationCompletePod);
				var manager:PodLayoutManager=PodLayoutManager(podHash[e.currentTarget]);
				manager.removeNullItems();
				manager.updateLayout(false);
			}
			catch(e:Error)
			{
				trace(e.errorID + "\n" + e.message);
			}
			
		}

		// Saves the pod content ViewStack state.
		private function onChangePodView(e:IndexChangedEvent):void
		{
			var pod:Pod=Pod(e.currentTarget);
			var viewId:String=PodLayoutManager(podHash[pod]).id;
			StateManager.setPodViewIndex(viewId, pod.id, e.newIndex);
		}

	}
}
